var BAM = {};
BAM.Tabs = new Class({ 
	collection: new Object(),
	tabHeadings: new Array(),
	tabContent: new Array(),
	openItem: 0,
	initialize: function(params) {		
		if(params) {
			// container element with class name "tab-collection"
			this.collection 	= 	params.tc;
			// Get references to the necessary dom elements
			this.tabHeadings 	= 	$ES(".tabs li", this.collection);
			this.tabContent 	= 	$ES(".tab-content", this.collection);
			
			if(this.tabHeadings.length == this.tabContent.length) {			
				this.openItem = params.dt || 0;
			
				// highlight default tab
				this.tabHeadings[this.openItem].className = "on";

				// hide all the content except for the initial item
				for(var i=0; i<this.tabContent.length; i++) {
					if(i != this.openItem) {
						this.tabContent[i].style.display = "none";
					}
				}
				
				// set up event listeners
				for(var i=0; i<this.tabHeadings.length; i++) {
					var obj = {};
					obj.clicked = i;
					this.tabHeadings[i].addEvent('click', this.switchTabs.bindWithEvent(this, obj));
				}

			} else {throw new Error("The number of headings, and content must be equal");}
		} else {throw new Error("You must pass a container element");}
	},
	switchTabs: function(e, params) {
		if(e != null) { e = new Event(e).stop(); }
		if(params.clicked >= this.tabHeadings.length) { 
			params.clicked = 0;
		} else if(params.clicked < 0) {
			params.clicked = this.tabHeadings.length -1;
		}
		if(this.openItem != params.clicked) {			
			this.tabHeadings[this.openItem].removeClass("on");
			this.tabHeadings[params.clicked].addClass("on");// = "on";
			this.tabContent[this.openItem].style.display = "none";
			this.tabContent[params.clicked].style.display = "block";	
			this.openItem = params.clicked;		
		}
	}
});
BAM.Tabs.Group = new Class({
	initialize: function() {
		document.addEvent('domready', this.gather.bind(this));
	},
	gather: function() {
		$$(".tab-collection").each(function(tabGroup){
			if(!tabGroup.hasClass("ignore")) {
				new BAM.Tabs({"tc":tabGroup});			
			}
		});
	}
});
new BAM.Tabs.Group();

BAM.ScrollBar = new Class({
	scrollBar: new Object(),
	knob: new Object(),
	slider: new Object(),
	scrollOffset: 140,
	
	initialize: function(params){
		if(params.scrollBar && params.knob && params.scrollArea && params.maxHeight) {
			this.scrollBar = params.scrollBar;
			this.knob = params.knob;
			var scrollArea = params.scrollArea;
			this.maxHeight = params.maxHeight;
			if(params.scrollOffset) {
				this.scrollOffset = params.scrollOffset;
			}
			var scrollHeight = scrollArea.getCoordinates().height;
			if(scrollHeight > this.maxHeight) {
				var slider = new Slider(this.scrollBar, this.knob, {	
					steps: (scrollHeight - this.scrollOffset),	
					mode: 'vertical',	
					onChange: function(step){
						scrollArea.style.top = - step + "px";
					}
				}).set(0);
			} else {
				this.hideScrollbar();
			}
		}
	},
	hideScrollbar: function() {
		this.scrollBar.style.display = "none";
	}
});